package com.example.communist_app_api.service.impl;

import com.example.communist_app_api.db.dao.OrganizationDao;
import com.example.communist_app_api.db.dao.UserOrganizationDao;
import com.example.communist_app_api.db.dto.UserOrganizationDTO;
import com.example.communist_app_api.db.pojo.User;
import com.example.communist_app_api.db.pojoDto.Organization;
import com.example.communist_app_api.service.UserOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Brother Sheng Editor
 * @version 1.0
 * Create by 2023/2/21 16:29
 */
@Service
public class UserOrganizationServiceImpl implements UserOrganizationService {
    @Autowired
    private UserOrganizationDao userOrganizationDao;

    @Autowired
    private OrganizationDao organizationDao;
    @Override
    public Integer joinCommunistByOrganizationIdAndUserId(Integer organizationId, Integer userId) {
        return userOrganizationDao.joinCommunistByOrganizationIdAndUserId(organizationId,userId);
    }

    @Override
    public String selectOrganizationNameByUserId(Integer userId) {
        return userOrganizationDao.selectOrganizationNameByUserId(userId);
    }

    @Override
    public Organization getOrganizationByUserId(Integer userId) {
        return userOrganizationDao.getOrganizationByUserId(userId);
    }

    @Override
<<<<<<< HEAD
<<<<<<< HEAD
=======
>>>>>>> 党建uniapp/dev
    public List<User> getAllUserByOrganizationIdBySelect(Integer organizationIdBySelect) {
        return userOrganizationDao.getAllUserByOrganizationIdBySelect(organizationIdBySelect);
    }

    @Override
    public Integer getUserNumInOrganizationByOrganizationId(Integer organizationId) {
        return userOrganizationDao.getUserNumInOrganizationByOrganizationId(organizationId);
    }

    @Override
    public void transformRelationshipByUserIdAndOrganizationId(Integer userId, Integer organizationId) {
        //TODO 组织转移之后，发布的文章等都需要转移
        userOrganizationDao.transformRelationshipByUserIdAndOrganizationId( userId,  organizationId);
    }

    @Override
    public List<UserOrganizationDTO> getUserOrganizationListByUserIdList(List<Integer> userIdList) {
        return userOrganizationDao.getUserOrganizationListByUserIdList(userIdList);
    }

    @Override
<<<<<<< HEAD
    public String getOrganizationLevelByUserId(Integer userId) {
        return userOrganizationDao.getOrganizationLevelByUserId(userId);
    }

    /**
     * 根据用户Id查询党总支下的所有人的Id作为List返回
     * @param userId
     * @return
     */
    @Override
    public List<Integer> getAllGeneralPartyBranchUserIdyBUserId(Integer userId) {
        Integer parentId = userOrganizationDao.getOrganizationByUserId(userId).getId();
        List<Integer> organizationIdList= organizationDao.getOrganizationIdListByParentId(parentId);
        organizationIdList.add(parentId);
        return userOrganizationDao.getAllGeneralPartyBranchUserIdByorganizationIdList(organizationIdList);
    }

    /**
     * 根据userId查询UserOrganization表中相同组织的所有用户
     * @param userId
     * @return
     */
    @Override
    public List<Integer> getAllUserIdByUserId(Integer userId) {
        return userOrganizationDao.getAllUserIdByUserId(userId);
    }

    @Override
    public Integer getAnnouncerCountByOrganizationAndUserId(Integer userId) {
        //判断是否是党总支
        Integer parentId = userOrganizationDao.getOrganizationByUserId(userId).getParentId();
        Integer grandfatherId = organizationDao.getOrganizationByOrganizationId(parentId).getParentId();
        //定义人数
        Integer returnCount = 0;
        if (grandfatherId == 0) {
            //判断是党总支
            //获取党总支+党支部的所有人数
            //获取organizationIdList
            List<Integer> organizationIdList = organizationDao.getOrganizationIdListByParentId(parentId);
            organizationIdList.add(parentId);
            //根据organizationIdList去查询userId
            returnCount = userOrganizationDao.getAllGeneralPartyBranchUserIdByorganizationIdList(organizationIdList).size();
        }else{
            //判断是党支部
             returnCount = userOrganizationDao.getAllUserIdByUserId(userId).size();
        }
        return returnCount;
    }


=======
=======
>>>>>>> 党建uniapp/dev
    public int getOrganizationIdByUserId(Integer userId) {
        return userOrganizationDao.getOrganizationIdByUserId(userId);
    }
>>>>>>> ead867a76a2d61b0673929f4330d549b1f515bf4
}
